{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "## Imports\n",
    "\n",
    "# utility modules\n",
    "import glob\n",
    "import os\n",
    "import sys\n",
    "import re\n",
    "\n",
    "# the usual suspects:\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# specialty modules\n",
    "import h5py\n",
    "import pyproj\n",
    "\n",
    "# run matplotlib in 'widget' mode\n",
    "%matplotlib widget\n",
    "%load_ext autoreload\n",
    "%autoreload 2\n",
    "\n",
    "from readers.read_HDF5_ATL03 import read_HDF5_ATL03\n",
    "from readers.get_ATL03_x_atc import get_ATL03_x_atc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# user inputs the ground track number and beam you want to find results for\n",
    "rgt = \"0531\"\n",
    "beam = 'gt1l'        #options:  gt1l, gt1r, gt2l, gt2r, gt3l, gt3r\n",
    "\n",
    "# folder path where tracks are stored, change this according to whether you are looking in the Greenland/Antarctica folder\n",
    "data_root = '/srv/shared/FirnAndMelt/ATL03/Antarctica/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Cycle 01 added\n",
      "Cycle 02 added\n",
      "Cycle 03 added\n",
      "Cycle 04 added\n",
      "Cycle 05 added\n",
      "Cycle 06 added\n",
      "cycle = 07, exception = list index out of range\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# list to store the data we want to plot\n",
    "plot_data = []\n",
    "\n",
    "# iterate over each repeat cycle\n",
    "for cycle in ['01','02','03','04','05','06','07']:\n",
    "\n",
    "    try:\n",
    "        # read the IS2 data with Tyler's ATL03 reader:\n",
    "        ATL03_file=glob.glob(os.path.join(data_root, f'*{rgt}{cycle}*.h5'))[0]\n",
    "        IS2_atl03_mds, IS2_atl03_attrs, IS2_atl03_beams =read_HDF5_ATL03(ATL03_file)\n",
    "\n",
    "        # add x_atc to the ATL03 data structure (this function adds to the LS2_ATL03_mds dictionary)\n",
    "        get_ATL03_x_atc(IS2_atl03_mds, IS2_atl03_attrs, IS2_atl03_beams)\n",
    "\n",
    "        #-- select the 2l beam from ATL03\n",
    "        D3 = IS2_atl03_mds[beam]\n",
    "        LMH=D3['heights']['signal_conf_ph'][:,3] >= 2\n",
    "\n",
    "        # add the data we will want to plot\n",
    "        #[x_atc all photons, h all photons, x_atc flagged, h flagged, cycle, date]\n",
    "        d = [ D3['heights']['x_atc'], D3['heights']['h_ph'], D3['heights']['x_atc'][LMH], D3['heights']['h_ph'][LMH], cycle, ATL03_file[-33:-29]+\"-\"+ATL03_file[-29:-27]+\"-\"+ATL03_file[-27:-25] ]\n",
    "        plot_data.append(d)\n",
    "        print(\"Cycle \" + cycle + \" added\")\n",
    "    \n",
    "    except Exception as e:\n",
    "        print(f'cycle = {cycle}, exception = {e}')\n",
    "        print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5c82be4a896c4d36a38deab4fa0a20f8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Canvas(toolbar=Toolbar(toolitems=[('Home', 'Reset original view', 'home', 'home'), ('Back', 'Back to previous …"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# create a scatter plot for each cycle\n",
    "plt.close()\n",
    "\n",
    "# these values change the locations you are looking at along the track\n",
    "x_min = 32071500\n",
    "x_max = 32073500\n",
    "y_min = 102\n",
    "y_max = 112\n",
    "\n",
    "fig, axes = plt.subplots(nrows=len(plot_data)//2, ncols=2, figsize=(10, 10), sharex=True, sharey=True)\n",
    "\n",
    "for c, ax in zip(plot_data, axes.flatten()):\n",
    "    ax.plot(c[0],c[1],'k.',markersize=0.25, label='Date: '+str(c[5]) + '  Cycle: ' + str(c[4]))\n",
    "    #ax.plot(c[2],c[3],'g.',markersize=0.5)\n",
    "    ax.legend(loc='upper right',)\n",
    "    #ax.set_ylabel('Height (m)')\n",
    "    ax.set_xlim((x_min,x_max))\n",
    "    ax.set_ylim((y_min,y_max))\n",
    "    \n",
    "fig.suptitle('Ground Track: ' + rgt, y=1)\n",
    "fig.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
